package com.tianyi.controller;

import com.tianyi.fileUpload.FileUploadService;
import com.tianyi.lib.model.WebResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Objects;

/**
 * 图片输出controller
 *
 * @author cuixuhui
 * @date 2020/06/20
 */
@RestController
@RequestMapping("photo")
public class PhotoOutputController extends BaseController {


    @Autowired
    private FileUploadService fileUploadService;

    /**
     * 获取原图
     *
     * @param imgName
     * @param response
     */
    @RequestMapping(value = "/getSourceImg")
    public void getSourceImg(String imgName, HttpServletResponse response) {

        String targetImgName = fileUploadService.getPhotoPath()+imgName;
        getImg(targetImgName, response);
    }

    /**
     * 获取缩略图
     *
     * @param imgName
     * @param response
     */
    @RequestMapping(value = "/getThumbnailsImg")
    public void getThumbnailsImg(String imgName, HttpServletResponse response) {

        String targetImgName = fileUploadService.getTmpPhotoPath()+imgName;
        getImg(targetImgName, response);
    }

    /**
     * 上传图片
     *
     * @param file
     */
    @RequestMapping(value = "/uploadPhoto")
    @ResponseBody
    public WebResult<String> uploadPhoto(MultipartFile file) {

        if (Objects.isNull(file)){
            return WebResult.error("图片为空");
        }
        String filePath = fileUploadService.uploadPhotoWithThumbnails(file);
        return WebResult.ok(filePath);
    }

    /**
     * 删除图片
     *
     * @param photoPath
     */
    @RequestMapping(value = "/deletePhoto")
    @ResponseBody
    public WebResult<String> deletePhoto(String photoPath) {

        if (StringUtils.isBlank(photoPath)){
            return WebResult.error("图片路径不能为");
        }
        fileUploadService.deletePhotoWithThumbnails(photoPath);
        return WebResult.ok("sucess");
    }

    public void getImg(String imgName, HttpServletResponse response) {
        FileInputStream fis = null;
        response.setContentType("image/gif");
        try {
            OutputStream out = response.getOutputStream();
            File file = new File(imgName);
            fis = new FileInputStream(file);
            byte[] b = new byte[fis.available()];
            fis.read(b);
            out.write(b);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
